﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow.BusinessImpl.Helpers
{
    public static class ExportHelper
    {
        /// <summary>
        /// 添加excel表头
        /// </summary>
        /// <param name="book"></param>
        /// <param name="sheet"></param>
        /// <param name="model">导出数据</param>
        /// <param name="startRow">开始的行号</param>
        /// <param name="displayColumns">要显示的列</param>
        public static void WriteSheetTitle(IWorkbook book, ISheet sheet, IList model, int startRow = 0, List<string> displayColumns = null, List<string> titleList = null)
        {
            if (model.Count > 0)
            {
                var firstLine = model[0];
                PropertyInfo[] properties = firstLine.GetType().GetProperties();
                if (properties != null && properties.Count() > 0)
                {
                    IRow row = sheet.CreateRow(startRow);
                    IRow row_next = sheet.CreateRow(startRow + 1);
                    ICellStyle style = book.CreateCellStyle();
                    style.VerticalAlignment = VerticalAlignment.Top;
                    style.Alignment = HorizontalAlignment.Center;

                    var parent = "";
                    var index = 0;
                    for (var i = 0; i < properties.Count(); i++)
                    {
                        var prop = properties[i];
                        var attr = Attribute.GetCustomAttribute(prop, typeof(InspectionTitleAttribute));
                        if (attr != null)
                        {
                            if (displayColumns != null && !displayColumns.Contains(prop.Name)) continue;
                            var titleAttr = (InspectionTitleAttribute)attr;
                            if (!string.IsNullOrEmpty(titleAttr.Title))
                            {
                                var title = titleAttr.Title;
                                if (titleList != null && !string.IsNullOrEmpty(titleList[index]))
                                {
                                    title = titleList[index];
                                }

                                //含有父节点
                                if (titleAttr.ParentCols > 0)
                                {
                                    if (titleAttr.ParentTitle != parent)
                                    {
                                        parent = titleAttr.ParentTitle;
                                        ICell cell_p = row.CreateCell(index);
                                        cell_p.SetCellValue(titleAttr.ParentTitle);
                                        cell_p.CellStyle = style;
                                        sheet.AutoSizeColumn(index, true);
                                        sheet.AddMergedRegion(new CellRangeAddress(startRow, startRow, index, titleAttr.ParentCols + index - 1));
                                    }
                                    ICell cell = row_next.CreateCell(index);
                                    cell.SetCellValue(title);
                                    cell.CellStyle = style;
                                    sheet.AutoSizeColumn(index, true);
                                }
                                else
                                {
                                    ICell cell = row.CreateCell(index);
                                    cell.SetCellValue(title);
                                    cell.CellStyle = style;
                                    sheet.AutoSizeColumn(index, true);

                                    sheet.AddMergedRegion(new CellRangeAddress(startRow, startRow + titleAttr.Rows - 1, index, index));
                                }
                                index++;
                            }
                        }
                    }
                }
            }
        }

        public static void WriteSheetData(IWorkbook book, ISheet sheet, IList model, int startRow = 1, List<string> displayColumns = null)
        {
            if (model.Count > 0)
            {
                var firstLine = model[0];
                PropertyInfo[] properties = firstLine.GetType().GetProperties();

                ICellStyle style = book.CreateCellStyle();
                style.VerticalAlignment = VerticalAlignment.Top;
                style.Alignment = HorizontalAlignment.Left;

                for (var i = 0; i < model.Count; i++)
                {
                    IRow row = sheet.CreateRow(startRow + i);
                    var index = 0;
                    for (var j = 0; j < properties.Count(); j++)
                    {
                        var prop = properties[j];
                        var attr = Attribute.GetCustomAttribute(prop, typeof(InspectionTitleAttribute));
                        if (attr != null)
                        {
                            if (displayColumns != null && !displayColumns.Contains(prop.Name)) continue;
                            var titleAttr = (InspectionTitleAttribute)attr;
                            if (!string.IsNullOrEmpty(titleAttr.Title))
                            {
                                var value = prop.GetValue(model[i], null);
                                ICell cell = row.CreateCell(index);
                                if (value != null && typeof(DateTime) == value.GetType())
                                {
                                    cell.SetCellValue(((DateTime)value == DateTime.MinValue || (DateTime)value == DateTime.MaxValue) ? "" : ((DateTime)value).ToString("yyyy-MM-dd hh:mm:ss"));
                                }
                                else
                                {
                                    cell.SetCellValue(value == null ? "" : value.ToString());
                                }
                                cell.CellStyle = style;
                                sheet.AutoSizeColumn(i, true);
                            }
                            index++;
                        }
                    }
                }

            }

        }
    }
}
